package com.swt.gap.controller;

import com.swt.gap.common.PageResult;
import com.swt.gap.model.entity.SysGroup;
import com.swt.gap.model.entity.SysGroupUser;
import com.swt.gap.service.SysGroupService;
import com.swt.gap.service.SysUserService;
import io.swagger.annotations.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author sx
 */
@Api(tags = "103 分组管理")
@RestController
@RequestMapping("/group")
public class SysGroupController {

    private final SysGroupService sysGroupService;
    private final SysUserService sysUserService;

    public SysGroupController(SysGroupService sysGroupService, SysUserService sysUserService) {
        this.sysGroupService = sysGroupService;
        this.sysUserService = sysUserService;
    }

    @ApiOperation(value = "新增分组")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "SysGroup", name = "sysGroup", required = true)
    })
    @ApiResponse(code = 201, message = "created", response = Integer.class)
    @PostMapping
    public ResponseEntity<Integer> addGroup(@RequestBody SysGroup sysGroup) {
        return ResponseEntity.status(HttpStatus.CREATED).body(sysGroupService.addGroup(sysGroup));
    }

    @ApiOperation(value = "修改分组")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "groupId", required = true),
            @ApiImplicitParam(paramType = "body", dataType = "SysGroup", name = "sysGroup", required = true)
    })
    @ApiResponse(code = 200, message = "ok", response = Integer.class)
    @PutMapping("/{groupId}")
    public ResponseEntity<Integer> updateDepartment(@PathVariable Integer groupId, @RequestBody SysGroup sysGroup) {
        sysGroup.setGroupId(groupId);
        return ResponseEntity.ok(sysGroupService.updateGroup(sysGroup));
    }

    @ApiOperation(value = "删除分组")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "groupId", required = true)
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = Integer.class),
            @ApiResponse(code = 442, message = "分组有绑定用户")
    })
    @DeleteMapping("/{groupId}")
    public ResponseEntity<Integer> deleteDepartment(@PathVariable Integer groupId) {
        if (!CollectionUtils.isEmpty(sysUserService.
                listUserByGroupId(1, 10, null, groupId).getList())){
            return ResponseEntity.status(442).build();
        }
        return ResponseEntity.ok(sysGroupService.deleteGroup(groupId));
    }


    @ApiOperation(value = "获全部分组")
    @ApiResponse(code = 200, message = "ok", response = SysGroup.class, responseContainer = "List")
    @GetMapping
    public ResponseEntity<List<SysGroup>> listAllGroup() {
        return ResponseEntity.ok(sysGroupService.listAllGroup());
    }

    @ApiOperation(value = "分组新增关联用户")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "SysGroupUser", name = "sysGroupUser", required = true)
    })
    @ApiResponse(code = 201, message = "created", response = Integer.class)
    @PostMapping("/user")
    public ResponseEntity<Integer> addGroupUser(@RequestBody SysGroupUser sysGroupUser) {
        return ResponseEntity.status(HttpStatus.CREATED).body(sysGroupService.addGroupUser(sysGroupUser));
    }

    @ApiOperation(value = "获取指定分组用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", dataType = "int", paramType = "query", value = "获取的当前页码，默认从第一页开始"),
            @ApiImplicitParam(name = "pageSize", dataType = "int", paramType = "query", value = "每页的条数"),
            @ApiImplicitParam(name = "keyword", dataType = "String", paramType = "query", value = "输入的用户名或者姓名"),
            @ApiImplicitParam(name = "groupId", dataType = "int", paramType = "query", value = "选择的分组Id")})
    @ApiResponses({@ApiResponse(code = 200, message = "用户列表",response = PageResult.class)})
    @GetMapping("/user")
    public ResponseEntity<PageResult> listGroupUser(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                             @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                             @RequestParam(value = "keyword", required = false) String keyword,
                                                             @RequestParam(value = "groupId") Integer groupId){


        return ResponseEntity.ok(sysUserService.listUserByGroupId(pageNum, pageSize, keyword, groupId));
    }


    @ApiOperation(value = "删除分组关联的用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", dataType = "int", paramType = "query", value = "选择的分组Id"),
            @ApiImplicitParam(name = "userId", dataType = "int", paramType = "query", value = "userId"),
    })
    @ApiResponse(code = 200, message = "ok", response = Integer.class)
    @DeleteMapping("/user")
    public ResponseEntity<Integer> deleteGroupUser(@RequestParam(value = "groupId") Integer groupId,
                                                   @RequestParam(value = "userId") Integer userId) {
        return ResponseEntity.ok(sysGroupService.deleteGroupUser(groupId, userId));
    }
}
